#include "rd522_if.h"
#include "rd522.h"
#include "buzzer.h"
//初始化IRQ PB9
 uint8_t g_lpcdirqflag = 0;

void ZKRD522_IrqPin_Config(void)
{
	EXTI_InitTypeDef EXTI_InitStructure;		//EXTIx中断线配置
	NVIC_InitTypeDef NVIC_InitStructure;		//EXTIx中断向量配置
	GPIO_InitTypeDef GPIO_InitStructure;
	
	//配置按键引脚为下降沿中断	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);	
	SYSCFG_EXTILineConfig(ZKRD522_IRQ_EXTI_PORTSOURCE,ZKRD522_IRQ_EXTI_PINSOURCE);	//将EXTI13指向PC13
	RCC_AHBPeriphClockCmd(ZKRD522_GPIO_IRQ_CLK, ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = ZKRD522_GPIO_IRQ_PIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = ZKRD522_GPIO_IRQ_Mode;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(ZKRD522_GPIO_IRQ_PORT, &GPIO_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel=ZKRD522_IRQ_EXTI_IRQ;
	NVIC_InitStructure.NVIC_IRQChannelPriority=0;
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	EXTI_InitStructure.EXTI_Line=ZKRD522_IRQ_EXTI_LINE;
	EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd=ENABLE;
	EXTI_Init(&EXTI_InitStructure);
}

//外部中断2服务程序
void ZKRD522_IRQPin_IRQHandler(void)
{
//	Buzzer_RunMul_Time();
	EXTI_ClearITPendingBit(EXTI_Line9);  //清除LINE0上的中断标志位
	g_lpcdirqflag = 1;					 //标志自动检卡中断产生
}

/**
  * @brief  Write lpcd reg through the UART/SPI/IIC interface into reg of PCD chip
  * @param  addr: addr to be writed.
  * @param  RegValue: val to be writed.
  * @retval none
  */
void write_lpcd_exreg(uint8_t addr, uint8_t RegValue)
{
#if defined ZKRD522_UART_Mode
	uart_write_reg(ex_reg, 0x40 + addr);
    uart_write_reg(ex_reg, 0xC0 + RegValue);
#elif defined ZKRD522_SPI_Mode
	spi_write_reg(ex_reg, 0x40 + addr);
	spi_write_reg(ex_reg, 0xC0 + RegValue);
#elif defined ZKRD522_IIC_Mode
	iic_write_reg(ex_reg, 0x40 + addr);
	iic_write_reg(ex_reg, 0xC0 + RegValue);
#else
	
#endif
}

/**
  * @brief  serial write data from register of chip
  * @param  addr: addr to be writed.
  * @retval reg value
  */
void write_buf(u8 addr, u8* data, uint32_t lenth) {
#if defined ZKRD522_UART_Mode
	uart_write_fifo(lenth,data);
#elif defined ZKRD522_SPI_Mode
	spi_write_fifo(data,lenth);

#elif defined ZKRD522_IIC_Mode
	iic_write_fifo(lenth,data);
#else
	
#endif
}

/**
  * @brief  read lpcd reg through the UART/SPI/IIC interface into reg of PCD chip
  * @param  addr: addr to be read.
  * @retval none
  */
uint8_t read_lpcd_exreg(uint8_t addr)
{
#if defined ZKRD522_UART_Mode
	uart_write_reg(ex_reg, 0x80 + addr);
	return uart_read_reg(ex_reg);
#elif defined ZKRD522_SPI_Mode
	spi_write_reg(ex_reg, 0x80 + addr);
	return spi_read_reg(ex_reg);
#elif defined ZKRD522_IIC_Mode
	iic_write_reg(ex_reg, 0x80 + addr);
	return iic_read_reg(ex_reg);
#else
	
#endif
//	return 0;
}

/**
 * @brief  serial Read data from register of chip
 * @param  addr: add of register
 * @param  lenth: the number of bytes shoud be read.
 * @retval None
 */

void read_buf(u8 addr, u8* data, uint32_t lenth) {
#if defined ZKRD522_UART_Mode
	uart_read_fifo(lenth,data);
#elif defined ZKRD522_SPI_Mode
	spi_read_fifo(data,lenth);
#elif defined ZKRD522_IIC_Mode
	iic_read_fifo(lenth,data);
#else
	
#endif
}
